package com.xuanyuanxinyu.core.task;



import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class TransformThreadUtils {

    private static volatile ThreadPoolExecutor executorService;

    private static final ThreadFactory S_THREAD_FACTORY = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "fileTrans #" + mCount.getAndIncrement());
        }
    };

    private synchronized static ThreadPoolExecutor getCachedThreadPool() {
        if (executorService == null) {
            synchronized (TransformThreadUtils.class) {
                if (executorService == null) {
                    executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L,
                            TimeUnit.SECONDS, new SynchronousQueue<>(), S_THREAD_FACTORY);
                }
            }
        }
        return executorService;
    }

    public static void execute(Runnable runnable) {
        getCachedThreadPool().execute(runnable);
    }

}
